home *** CD-ROM | disk | FTP | other *** search
- /*
- File: HeapSort.cp
-
- Contains: Apply a Heap Sort Algorithm to an offscreen, scrambled GWorld
-
- Written by:
-
- Copyright: Copyright © 1988-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/27/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
-
-
- */
- #include "SortPicts.h"
-
- void HeapSort( SortPicts *sortPicts)
- {
- sortPicts->HeapSort();
- }
-
- /*************************************************************/
- /* */
- /* The Actual Sort Algorithms */
- /* */
- /*************************************************************/
-
- void SortPicts::HeapSort( void)
- {
-
- long loop;
- long size;
-
- BuildHeap();
-
- size = N - 1;
- for( loop = N-1; loop; --loop)
- {
- ExchangeSortItem( 0, loop);
-
- Heapify( --size, 0);
- }
- }
-
- void SortPicts :: BuildHeap( void)
- {
- long loop;
-
- for( loop = (N>>1)+3; loop; --loop)
- Heapify( N-1, loop -1);
- }
-
- //
- // This code adjusts for a zerø based array
- // (Where as the original source from Sedgewick (good book)
- // was 1..N, not 0..N-1
- //
-
- #define Left(i) (((i+1) << 1) -2)
- #define Right(i) (((i+1) << 1) -1)
-
- void SortPicts :: Heapify( long size, long index)
- {
- long left, right, largest;
-
- left = Left( index);
- right = Right( index);
-
- largest = index;
-
- if( left <= size)
- if( sortData[left] > sortData[index])
- largest = left;
-
- if( right <= size)
- if( sortData[right] > sortData[largest])
- largest = right;
-
- if( largest != index)
- {
- ExchangeSortItem(index, largest);
- Heapify( size, largest);
- }
- }
-
-